﻿using System;
using System.Collections.Generic;
using System.Text;
using StarBotDotNet;

namespace StarBotDotNet.BuildStateCheckers
{
    public class StateCheckerBuild:StateCheckerAbstract
    {
        protected override bool CanEnterThisState(IBuildingParty buildingParty, BuildTarget target, StarBotDotNet.ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {
            if (target.UnitToBuild.Building)
            {
                if (target.UnitToBuild != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }

            


        }

        protected override void AfterMovingToState(ProxyBot bot, IBuildingParty buildingParty, BuildTarget target)
        {
            bot.Player.DeAlocateResources(target.UnitToBuild.MineralsCost, target.UnitToBuild.GasCost);
        }

        protected override bool CanTryToMoveToThisState(IBuildingParty buildingParty, BuildTarget target, StarBotDotNet.ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {
            if (target.Builder.Unit.IsConstructing || target.Builder.Unit.IsTraining)
            {
                return true;
            }
            return false;
        }



        protected override void PrepareInitiationOfMovingToThisState(IBuildingParty buildingParty, BuildTarget target, StarBotDotNet.ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {


            if (target.Builder.Unit.IsConstructing || target.Builder.Unit.IsTraining)
            {
                return;

            }



            if (!target.BuildLocationValid(currentUnits, bot))
            {
                target.FindNewBuildLocation(bot, currentUnits);

            }

            if (target.UnitToBuild.Building)
            {


                if (target.LocationReached())
                {

                    if (target.Builder.IsIdle)
                    {
                        target.InitiateBuilding(bot, currentTime);
                    }
                }
                else
                {
                    if (target.Builder.IsIdle)
                    {
                        bot.rightClick(target.Builder.Unit.ID, target.BuildLocation.X, target.BuildLocation.Y);
                    }
                }
            }
            else
            {

                target.InitiateBuilding(bot, currentTime);

            }

            //            if (target.LocationReached())
            //            {
            //                target.InitiateBuilding(bot, currentTime);
            //            }
            //            else
            //            {
            //                if (target.Builder.IsIdle)
            //                {
            //                    bot.rightClick(target.Builder.Unit.ID, target.BuildLocation.X, target.BuildLocation.Y);
            //                }
            //            }
        }

        public override BuildStateEnum StateFrom
        {
            get 
            {
                return BuildStateEnum.b2_BuilderAndResourcesAllocated;

            }
        }

        public override BuildStateEnum StateTo
        {
            get { return BuildStateEnum.b5_UnderProduction; }
        }

        protected override void InitiateMovingToThisState(IBuildingParty buildingParty, BuildTarget target, StarBotDotNet.ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {
            //target.MoveToNextState(StateTo);
        }
    }
}
